#include "pch.h"
#include "GraphicShape.h"
#include <string>
#include "GraphicDefine.h"

#include <GL/GL.h>
#include <GL/GLU.h>
#include <GL/glut.h>

CGraphicEllipse::CGraphicEllipse(const CPoint& startScreenPoint, const CPoint& endScreenPoint, const std::function<float(float, int)>& fn) :
	CGraphicShapeBase(startScreenPoint, endScreenPoint, fn)
{
	emShape = SE_ELLIPSE;
	m_red = 128.0f;
	m_green = 0.0f;
	m_blue = 128.0f;
}

CGraphicEllipse::~CGraphicEllipse()
{
}

void CGraphicEllipse::Draw()
{
	drawByEquation();
}

bool CGraphicEllipse::AtGraphBorder(const CPoint& curPoint)
{
	float xRadius = abs(m_startPoint.x - m_endPoint.x);
	float yRadius = abs(m_startPoint.y - m_endPoint.y);

	float x = m_fnChangePos(curPoint.x, 1);
	float y = m_fnChangePos(curPoint.y, 2);

	/*char szBuff[1024] = { 0 };
	sprintf_s(szBuff, "m_startPoint.x = %d at AtGraphBorder\n", m_startPoint.x);
	OutputDebugString(szBuff);*/

	float y1 = m_startPoint.y + yRadius * sqrt(1.0f - ((x - m_startPoint.x) / xRadius)*((x - m_startPoint.x) / xRadius));
	float y2 = m_startPoint.y - yRadius * sqrt(1.0f - ((x - m_startPoint.x) / xRadius)*((x - m_startPoint.x) / xRadius));
	if ((abs(y1 - y) < 0.015) || (abs(y2 - y) < 0.015))
	{
		return true;
	}

	return false;
}

void CGraphicEllipse::drawByEquation()
{
	float xRadius  = abs(m_startPoint.x - m_endPoint.x);
	float yRadius  = abs(m_startPoint.y - m_endPoint.y);

	glColor3f(m_red, m_green, m_blue);
	glBegin(GL_LINE_STRIP);

	for ( float x =-xRadius; x<=xRadius; x+=0.01f )
	{
		float y = yRadius * sqrt(1.0f- float(x/xRadius)*float(x/xRadius));
		glVertex2f(m_startPoint.x + x, m_startPoint.y+y);
	}

	bool b = false;
	for ( float x =xRadius; x>=-xRadius; x-=0.01f )
	{
		float y = -yRadius * sqrt(1.0f - float(x/xRadius)*float(x/xRadius));
		glVertex2f(m_startPoint.x + x, m_startPoint.y+y);
	}

	glVertex2f(m_startPoint.x -xRadius, m_startPoint.y);

	glEnd();
}
